package com.winit.openapi.wh.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.query.Page;
import com.winit.oms.spi.inbound.InboundOrderMergeService;
import com.winit.oms.spi.inbound.command.InboundOrderMergeCancelCommand;
import com.winit.oms.spi.inbound.command.InboundOrderMergeCommand;
import com.winit.oms.spi.inbound.command.QuerySubInboundOrderByMainOrderCommand;
import com.winit.oms.spi.inbound.command.SearchInboundOrderCommand;
import com.winit.oms.spi.inbound.vo.InboundOrderVo;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.wh.builder.InboundOrderMergeBuilder;
import com.winit.openapi.wh.validator.InboundOrderMergeValidator;

/**
 * 海外仓入库单合并
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * bing.gu 	1.0  		2016年1月25日 	Created
 *
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/wh/mergeOrder")
public class InboundOrderMergeController extends BaseController {

    @Resource
    private InboundOrderMergeService inboundOrderMergeService;

    /**
     * 查询可合并订单(支持分页)
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/query")
    public String query(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        SearchInboundOrderCommand command = new SearchInboundOrderCommand();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            InboundOrderMergeValidator.validCanMergeQueryParam(json);
            command = InboundOrderMergeBuilder.buildQueryParam(json);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        // 调用接口查询
        Page<InboundOrderVo> orderPage = inboundOrderMergeService.queryOrderCanMergeByPage(command);
        if (orderPage != null) {
            Map<String, Object> data = new HashMap<String, Object>();
            List<Map<String, Object>> datalist = InboundOrderMergeBuilder.buildOrderList(orderPage.getContent());
            if ("P".equals(command.getQueryMode())) {
                Map<String, Object> pageMap = getPageParams(orderPage);
                data.put("pageParams", pageMap.get("pageParams"));
            }
            data.put("orders", datalist);
            responseMsg.setData(data);
        }
        return SUCCESS;
    }

    /**
     * 海外仓入库单合并接口
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/merge")
    public String merge(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        InboundOrderMergeCommand command = new InboundOrderMergeCommand();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            InboundOrderMergeValidator.validMergeParam(json);
            command = InboundOrderMergeBuilder.buildMergeCommand(json);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        // 调用接口创建入库单
        inboundOrderMergeService.merge(command);
        return SUCCESS;
    }
    
    /**
     * 海外仓入库单合并取消接口
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/mergeCancel")
    public String mergeCancel(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        InboundOrderMergeCancelCommand command = new InboundOrderMergeCancelCommand();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            InboundOrderMergeValidator.validMergeCancelParam(json);
            command = InboundOrderMergeBuilder.buildMergeCancelCommand(json);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        // 调用接口创建入库单
        inboundOrderMergeService.cancelOrderMerge(command);
        return SUCCESS;
    }

    /**
     * 根据主单号查询子单列表（支持分页）
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/querySubOrders")
    public String querySubOrders(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        QuerySubInboundOrderByMainOrderCommand command = new QuerySubInboundOrderByMainOrderCommand();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            InboundOrderMergeValidator.validQuerySubOrderByMainOrderParam(json);
            command = InboundOrderMergeBuilder.buildQuerySubOrderByMainOrderCommand(json);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        // 调用接口创建入库单
        Page<InboundOrderVo> orderPage = inboundOrderMergeService.querySubOrderPageByMainNo(command);
        if (orderPage != null) {
            Map<String, Object> data = new HashMap<String, Object>();
            List<Map<String, Object>> datalist = InboundOrderMergeBuilder.buildOrderList(orderPage.getContent());
            if ("P".equals(command.getQueryMode())) {
                Map<String, Object> pageMap = getPageParams(orderPage);
                data.put("pageParams", pageMap.get("pageParams"));
            }
            data.put("orders", datalist);
            responseMsg.setData(data);
        }
        return SUCCESS;
    }

}
